//
//  LoginViewController.m
//  CarPark
//
//  Created by oceans on 2019/7/24.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "LoginViewController.h"
#import "TextField.h"

@interface LoginViewController ()

@property (strong, nonatomic) TextField *userNameField;
@property (strong, nonatomic) TextField *codeField;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) double timertime;
@property (strong, nonatomic) UIScrollView *scrollView;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    if (@available(iOS 11.0, *)) {
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
//    button.frame = CGRectMake(ScreenWidth-70.0, XStatusBarHeight+20.0, 40.0, 40.0);
//    [button setImage:[UIImage imageNamed:@"chacha"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(forBack) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight - XNavigationHeight)];
    [self.view addSubview:_scrollView];
    
    self.userNameField = [[TextField alloc] initWithFrame:CGRectMake(15, 10, ScreenWidth-30, 45) type:TFTLeftView];
    _userNameField.clearButtonMode = UITextFieldViewModeAlways;
    _userNameField.leftLabel.text = @"手机号";
    _userNameField.delegate = self;
    [self.scrollView addSubview:_userNameField];
    
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0]}];
    _userNameField.attributedPlaceholder = string;
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(15, CGRectGetMaxY(self.codeField.frame)+71, ScreenWidth - 30, 45);
    login.titleLabel.font = [UIFont systemFontOfSize:14];
    login.backgroundColor = COLOR(238, 153, 112, 1);
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [login setTitle:@"登  录" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(toLogin) forControlEvents:UIControlEventTouchUpInside];
    login.layer.cornerRadius = 5;
    login.layer.masksToBounds = YES;
    [self.scrollView addSubview:login];
}

- (TextField *)codeField {
    
    if (!_codeField) {
        
        _codeField = [[TextField alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.userNameField.frame)+10, ScreenWidth-30, 45) type:TFTCountDownButton];
        _codeField.clearButtonMode = UITextFieldViewModeAlways;
        _codeField.leftLabel.text = @"验证码";
        [_codeField.rightButton setTitle:@"获取验证码" forState:UIControlStateNormal];
//        _codeField.delegate = self;
        [_codeField.rightButton addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:_codeField];
        NSAttributedString *string2 = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0]}];
        _codeField.attributedPlaceholder = string2;
    }
    
    return _codeField;
}

- (void)getCode {
    
    __weak typeof(self) weakself = self;
    if ([Utility checkMobile:_userNameField.text]) {
        [self.view endEditing:YES];
        _timertime = 60;
        [self startTimer];
        [[UserModel shared] getCodeWithPhoneNumber:_userNameField.text scope:@"login" successBlock:^{
            
        } failedBlock:^{
            [weakself stopTimer];
        }];
    }
}

- (void)startTimer {
    
    self.codeField.rightButton.enabled = NO;
    [self.codeField.rightButton setTitle:[NSString stringWithFormat:@"%.0fs 后重新发送", _timertime] forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}

- (void)timerMethod {
    
    if (_timertime <= 0) {
        [self.codeField.rightButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeField.rightButton.enabled = YES;
        [self.timer invalidate];
        return;
    }
    _timertime = _timertime - 1;
    [self.codeField.rightButton setTitle:[NSString stringWithFormat:@"%.0fs 后重新发送", _timertime] forState:UIControlStateNormal];
}

- (void)stopTimer {
    [self.codeField.rightButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.codeField.rightButton.enabled = YES;
    [self.timer invalidate];
}

- (void)toLogin {
    
    if ([Utility checkMobile:_userNameField.text] && _codeField.text.length > 0) {
        [self.view endEditing:YES];
        __weak typeof(self) weakself = self;
        [[UserModel shared] loginWithCode:_codeField.text username:self.userNameField.text successBlock:^{
            [weakself forBack];
        }];
    }
}

- (void)forBack {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
